#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>

#define SIZE 4096

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("useage %s filename", argv[0]);
        return 1;
    }
    int fd = open(argv[1], O_CREAT | O_RDWR, 0666);
    // 将log.txt的大小扩为4096字节，以0填充，以确保能正确地被mmap映射
    ftruncate(fd, SIZE);
    char *addr = (char *)mmap(nullptr, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (addr == (char *)-1)
    {
        perror("mmap");
    }
    printf("addr: %p\n", addr);
    for (size_t i = 0; i < 10; i++)
    {
        addr[i] = 'a';
    }
    munmap(addr, 4096);
    close(fd);
    return 0;
}